#include "orangepch.h"
#include "OrthogonalCameraController.h"
#include "Core/Input.h"
#include "Core/KeyCode.h"
#include "Core/Log.h"
namespace Orange {



	OrthogonalCameraController::OrthogonalCameraController(float aspectRadio, bool rotation) :m_aspectRadio(aspectRadio),
		m_Camera(-aspectRadio * m_ZoomLevel, aspectRadio* m_ZoomLevel, -m_ZoomLevel, m_ZoomLevel), m_Rotation(rotation)
	{
	}

	OrthogonalCameraController::~OrthogonalCameraController()
	{
	}

	void OrthogonalCameraController::OnUpdate(Time detalTime)
	{
		if (Input::isKeyPressed(ORANGE_KEY_A)) {
			m_Camera_Position.x -= m_CameraMoveSpeed * detalTime;
		}
		else if (Input::isKeyPressed(ORANGE_KEY_D)) {
			m_Camera_Position.x += m_CameraMoveSpeed * detalTime;
		}
		else if (Input::isKeyPressed(ORANGE_KEY_W)) {
			m_Camera_Position.y -= m_CameraMoveSpeed * detalTime;
		}
		else if (Input::isKeyPressed(ORANGE_KEY_S)) {
			m_Camera_Position.y += m_CameraMoveSpeed * detalTime;
		}

		if (m_Rotation) {
			if (Input::isKeyPressed(ORANGE_KEY_Q)) {
				m_Camera_Rotation -= m_CameraRotateSpeed * detalTime;
			}
			else if (Input::isKeyPressed(ORANGE_KEY_E)) {
				m_Camera_Rotation += m_CameraRotateSpeed * detalTime;
			}
			m_Camera.SetRotation(m_Camera_Rotation);
		}
		m_Camera.SetPosition(m_Camera_Position);
	}

	void OrthogonalCameraController::OnEvent(Event& e)
	{
		EventDispatcher dispatch(e);
		dispatch.Dispatch<MouseScrolledEvent>(ORANGE_BIND_FN(OrthogonalCameraController::OnMouseScrollEvent));
		dispatch.Dispatch<WindowResizeEvent>(ORANGE_BIND_FN(OrthogonalCameraController::OnWindowResizeEvent));
	}

	void OrthogonalCameraController::OnResize(uint32_t width, uint32_t height)
	{
		m_aspectRadio = (float)width / (float)height;

		m_Camera.SetPrejectMatrix(-m_aspectRadio * m_ZoomLevel, m_aspectRadio * m_ZoomLevel, -m_ZoomLevel, m_ZoomLevel);

	}

	bool OrthogonalCameraController::OnMouseScrollEvent(MouseScrolledEvent& e)
	{
		m_ZoomLevel -= (float)e.GetOffsetY() * 0.25;

		m_Camera.SetPrejectMatrix(-m_aspectRadio * m_ZoomLevel, m_aspectRadio * m_ZoomLevel, -m_ZoomLevel, m_ZoomLevel);
		return false;
	}

	bool OrthogonalCameraController::OnWindowResizeEvent(WindowResizeEvent& e)
	{
	
		OnResize(e.GetWidth(), e.GetHeight());
		return false;
	}

}